import {ChangeDetectorRef, Component} from '@angular/core';
import {LoadingController, NavController, NavParams} from 'ionic-angular';
import {JidihttpProvider} from "../../providers/jidihttp/jidihttp";

/**
 * Generated class for the BaikelistPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */
@Component({
  selector: 'page-baikelist',
  templateUrl: 'baikelist.html',
})
export class BaikelistPage {
  /** baike **/
  baikeMoviesbozhu: Array<any>=[];
  /** baike **/
  tasklistbaike:Array<any>=[];//list列表
  /** 分页 **/
  pageno:number=1;
  loading: any;
  infiniteEvent:any;//上拉加载的全局事件
  constructor(public navCtrl: NavController, public navParams: NavParams,public jidiProvider:JidihttpProvider,public cdchange:ChangeDetectorRef,public loadingCtrl:LoadingController) {
    /** http请求 **/
    this.baikehttpClinet();
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad BaikelistPage');
  }

  /** http 请求 **/

  baikehttpClinet(){
    this.jidiProvider.getBaikeJIangtangHttp(this.pageno).subscribe(data => {
      if(data['data']['records']['length']>0){
        for(let i=0;i<data['data']['records']['length'];i++){
          this.baikeMoviesbozhu.push(data['data']['records'][i]);
        }
        //alert(this.jidiMovies);
        this.tasklistbaike=this.baikeMoviesbozhu;
        this.cdchange.detectChanges();
      }
    },error => {

    });


  }

  /** 分页请求 **/
  /** 分页请求 **/
  doInfinite(infinite) {
    this.loading = this.loadingCtrl.create({
      content: '努力加载中...'
    });
    this.loading.present();


    this.infiniteEvent = infinite;
    this.pageno += 1;
    this.jidiProvider.getBaikeJIangtangHttp(this.pageno).subscribe(data => {
      if (this.loading) {
        this.loading.dismiss();
      }
      if(this.infiniteEvent){
        if(data['data']['records']['length']<1){
          this.infiniteEvent.enable(false);
        }
        for(let i=0;i<data['data']['records']['length'];i++){
          this.baikeMoviesbozhu.push(data['data']['records'][i]);
        }
        this.tasklistbaike=this.baikeMoviesbozhu;
        this.infiniteEvent.complete();
        this.cdchange.detectChanges();
      }
    },error => {

    });

  }

}
